<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/4/16
 * Time: 16:37
 */
namespace Assistant\Controller;
use Admin\Controller\AdminController;

class ExpenseController extends AdminController
{
    //物业费列表
    public function lst(){
        $year = D('Admin/Dict')->getDict('year');
        $data = D('Expense')->expenseSearch();
//        echo '<pre>';
//        var_dump(I('param.'));
//        var_dump($data);die;
        $assign = array(
            'page' => $data['page'],
            'data' => $data['data'],
            'year' => $year
        );

        $this->assign($assign);
        $this->display();
    }


    //物业费详情
    public function detail(){
        $id = I('param.id');
        $roomId = I('param.room_id');
        $model = D('Expense');
        $data = $model->find($id);
        $this->assign('data', $data);
//        var_dump($data);die;
        $this->display();
    }
    
    //房间列表
    public function add(){
        $data = D('Expense')->roomSearch();
//        myDump($data);die;
        $communityData = getCommunityData();
        $assign = array(
            'page' => $data['page'],
            'data' => $data['data'],
            'communityData' => $communityData
        );
        
        $this->assign($assign);
        $this->display();
    }

    //删除物业费
    public function delete(){
    	$id = I('param.id');
        $roomId = I('param.room_id');
    	$model = D('Expense');
    	if ($model->delete($id) !== FALSE)
    	{
            $this->success('删除成功！');
            exit;
    	}
    	else 
    	{
            $this->error($model->getError());
    	}
    }

    //添加物业费
    public function adding(){
        $roomId = I('param.room_id');
         if (IS_POST)
    	{
            $model = D('Expense');
            if ($model->create())
            {
                if ($id = $model->add()) {
                    $this->redirect(U('adding',array('room_id'=>$roomId)));
//                    $data['status'] = 1;
//                    $this->ajaxReturn($data); // 成功
                }
            }
            $this->error($model->getError());
//            $data['status'] = 0;
//            $data['sign'] = ($model->getError());
//            $this->ajaxReturn($data);
        }
        $year = D('Admin/Dict')->getDict('year');
        $type = D('Admin/Dict')->getDict('expenseType');
        $data = D('Expense')->readRoom();
        $communityData = D('Expense')->readCommunity();
        $roomData = $data['data'][0]['Room'];
        if (!$roomData)
            $roomData = M('Room')->where(array('id'=>$roomId))->find();
        $assign = array(
            'year' => $year,
            'type' => $type,
            'data' => $data['data'],
            'page' => $data['page'],
            'roomData'=>$roomData,
            'communityData'=>$communityData
        );
//        echo '<pre>';
//        var_dump($data);
//        var_dump($communityData);
//        var_dump($roomData);
//        var_dump($year);
//        var_dump($type);die;
        $this->assign($assign);   
        $this->display();
    }

    //物业费编辑
    public function edit(){
        $roomId = I('param.room_id');
        $id = I('param.id');
         if (IS_POST)
    	{
            $model = D('Expense');
            if ($model->create())
            {
                if ($model->save()){
//                    $this->redirect(U('edit',array('id'=>$id,'room_id'=>$roomId)));
                    $data['status'] = 1;
                    $this->ajaxReturn($data); // 成功
                }
            }
//            $this->error($model->getError());
            $data['status'] = 0;
            $data['sign'] = ($model->getError());
            $this->ajaxReturn($data);
        }
        $year = D('Admin/Dict')->getDict('year');
        $type = D('Admin/Dict')->getDict('expenseType');
        $roomData = M('Room')->find($roomId);

        $id = I('param.id');
        $model = D('Expense');
        $data = $model->find($id);

        $assign = array(
            'year' => $year,
            'type' => $type,
            'data' => $data,
            'roomData'=>$roomData
        );
//        echo '<pre>';
//        var_dump($roomId);
//        var_dump($data);
//        var_dump($roomData);
//        var_dump($year);
//        var_dump($type);die;
        $this->assign($assign);
        $this->display();
    }

    //物业费缴费
    public function pany(){
        $roomId = I('param.room_id');
        $id = I('param.id');

        $where['room_id'] = $roomId;
        $date['status'] = 1;
        $isok = M('Expense')->where($where)->data($date)->save();
        if($isok);
    }

    //设置小区物业费缴纳时间
    public function payTime(){
        $data['expense_start_time'] = I('expense_start_time');
        $data['expense_end_time'] = I('expense_end_time');
        $data['expense_close_time'] = I('expense_close_time');
        $community_id = I('community_id');
        $model = M('Community');
        $is_ok = $model->data($data)->where(array('id'=>$community_id))->save();
        if ($is_ok) {
            $this->success('设置成功',U('add'),1);
        } else
        $this->error($model->getError());
    }

    public function chooseExcel(){
        $year = D('Admin/Dict')->getDict('year');
        $assign = array(
            'year' => $year
        );
        $this->assign($assign);

        $this->display();
    }

    //生成excel统计
    public function dumpExcel(){
        $model = D('Expense');
        $data = $model->expenseSearch(99999,1,false,false,false)['data'];
//        echo '<pre>';
//        var_dump($data);die;
//        $excel = new \ZL\Excel();
//        $excel->dumpExcel($expenseData);
        $type = D('Admin/Dict')->getDict('expenseType');
        $arr = array();
        foreach ($type as $v) {
            $arr['type'][$v['value']] = $v['text'];
        }
        $payWay = D('Admin/Dict')->getDict('payWay');
        foreach ($payWay as $v) {
            $arr['payWay'][$v['value']] = $v['text'];
        }

        $title = array('小区','园区','楼房','单元','房间','支付人','支付人电话','支付时间','支付方式','支付状态','支付金额','类型','年度','开始时间','结束时间','截止时间','微信支付人');
        foreach ($data as $k=>$v){
            foreach ($v as $k1=>$v1){
                if ($k1=='is_edit' || $k1=='wsq_payer_id' || $k1=='update_time' || $k1=='id' || $k1=='no' || $k1=='room_name')
                    continue;
                $_data[$k][$k1] = $model->kvToName($k1, $v1, $arr);
            }
        }
//        echo '<pre>';
//        var_dump($_data);die;

        dumpExcel($title,$_data);
    }

}